﻿using System;
using System.Web.UI.WebControls;
using DBProvider.Providers;
using DBProvider;

namespace HealthCommunity.Controls
{
	public partial class UCMemberDetail : System.Web.UI.UserControl
	{
		private IUserProvider _UserProvider;

		public int UserID
		{
			get
			{
				return CommonHelper.QueryStringInt("id");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindUser();
			}
		}

		protected void lbtnUpdateCredit_Click(object sender, EventArgs e)
		{
			if (_UserProvider == null)
			{
				_UserProvider = new UserProvider();
			}

			var theUser = _UserProvider.GetUser(UserID, true);

			if (theUser != null)
			{
				var theCredit = theUser.Credits.GetValueOrDefault(0);

				if (int.TryParse(txtCredits.Text.Trim(), out theCredit))
				{
					theUser.Credits = theCredit;

					if (_UserProvider.UpdateUser(theUser, false))
					{
						lblMsg.Text = "积分设置成功";
					}
					else
					{
						lblMsg.Text = "积分设置失败了";
					}
				}
			}
		}

		protected void btnState_Click(object sender, EventArgs e)
		{
			var btnState = sender as Button;

			if (_UserProvider == null)
			{
				_UserProvider = new UserProvider();
			}

			var theUser = _UserProvider.GetUser(UserID, false);

			if (theUser != null)
			{
				if (btnState.CommandName.Equals("active"))
				{
					theUser.UserState = (int)UserState.Active;

					if (_UserProvider.UpdateUser(theUser, false))
					{
						ltrUserState.Text = "有效";
						btnState.Text = "删除";
						btnState.CommandName = "delete";
					}
				}
				else if (btnState.CommandName.Equals("delete"))
				{
					theUser.UserState = (int)UserState.Deleted;

					if (_UserProvider.UpdateUser(theUser, false))
					{
						ltrUserState.Text = "已删除";
						btnState.Text = "激活";
						btnState.CommandName = "active";
					}
				}
			}
		}

		private void BindUser()
		{
			if (UserID > 0)
			{
				if (_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				var theUser = _UserProvider.GetUser(UserID, true);

				if (theUser != null)
				{
					this.ltrAddress.Text = string.Format("{0} {1} {2}", theUser.StateProvince != null ? theUser.StateProvince.Name : string.Empty, theUser.City != null ? theUser.City.CityName : string.Empty, theUser.Address);
					this.ltrBirthDate.Text = theUser.Birthdate.HasValue ? theUser.Birthdate.Value.ToShortDateString() : string.Empty;
					this.ltrCName.Text = theUser.UserCName;
					this.ltrPosition.Text = theUser.Position;
					this.ltrEMail.Text = theUser.Email;
					this.ltrFax.Text = theUser.Fax;
					this.ltrMSN.Text = theUser.MSN;
					this.ltrPhone.Text = theUser.PhoneNumber;
					this.ltrQQ.Text = theUser.QQ;
					this.ltrRemark.Text = theUser.Remark;
					if (theUser.Sex == 1)
					{
						this.ltrSex.Text = "男";
					}
					else if (theUser.Sex == 2)
					{
						this.ltrSex.Text = "女";
					}
					else if (theUser.Sex == 0)
					{
						this.ltrSex.Text = "保密";
					}
					this.ltrSFZ.Text = theUser.ShengFenZheng;
					this.ltrTel.Text = theUser.Telephone;
					this.ltrUserName.Text = theUser.UserName;
					this.ltrZip.Text = theUser.ZipCode;

					if (theUser.UserState == (int)UserState.Active)
					{
						this.ltrUserState.Text = "有效";
						btnState.Text = "删除";
						btnState.CommandName = "delete";
					}
					else
					{
						this.ltrUserState.Text = "已删除";
						btnState.Text = "激活";
						btnState.CommandName = "active";
					}

					txtCredits.Text = theUser.Credits.GetValueOrDefault(0).ToString();
				}
			}
		}
	}
}